package com.zzyl.nursing.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.zzyl.common.core.domain.R;
import com.zzyl.nursing.dto.CheckOutApplyDto;
import com.zzyl.nursing.vo.CheckOutDetailVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.nursing.domain.CheckOut;
import com.zzyl.nursing.service.ICheckOutService;
import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.common.core.page.TableDataInfo;

/**
 * 退住办理Controller
 *
 * @Author: Zhy
 * @Date: 2025-07-13
 */
@Api(tags = "退住办理管理")
@RestController
@RequestMapping("/nursing/checkOut")
@RequiredArgsConstructor
public class CheckOutController extends BaseController {

    private final ICheckOutService checkOutService;

    /**
     * 查询退住办理列表
     */
    @ApiOperation("查询退住办理列表")
    @PreAuthorize("@ss.hasPermi('nursing:checkOut:list')")
    @GetMapping("/list")
    public TableDataInfo<List<CheckOut>> list(@ApiParam("退住办理查询条件") CheckOut checkOut) {
        startPage();
        List<CheckOut> list = checkOutService.selectCheckOutList(checkOut);
        return getDataTable(list);
    }

    /**
     * 导出退住办理列表
     */
    @ApiOperation("导出退住办理列表")
    @PreAuthorize("@ss.hasPermi('nursing:checkOut:export')")
    @Log(title = "退住办理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@ApiParam(value = "退住办理查询条件") HttpServletResponse response, CheckOut checkOut) {
        List<CheckOut> list = checkOutService.selectCheckOutList(checkOut);
        ExcelUtil<CheckOut> util = new ExcelUtil<>(CheckOut.class);
        util.exportExcel(response, list, "退住办理数据");
    }

    /**
     * 获取退住办理详细信息
     */
    @ApiOperation("获取退住办理详细信息")
    @PreAuthorize("@ss.hasPermi('nursing:checkOut:query')")
    @GetMapping(value = "/{id}")
    public R<CheckOut> getInfo(@ApiParam("退住办理ID") @PathVariable("id") Long id) {
        return R.ok(checkOutService.selectCheckOutById(id));
    }

    /**
     * 新增退住办理
     */
    @ApiOperation("新增退住办理")
    @PreAuthorize("@ss.hasPermi('nursing:checkOut:add')")
    @Log(title = "退住办理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@ApiParam("退住办理信息") @RequestBody CheckOut checkOut) {
        return toAjax(checkOutService.insertCheckOut(checkOut));
    }

    /**
     * 修改退住办理
     */
    @ApiOperation("修改退住办理")
    @PreAuthorize("@ss.hasPermi('nursing:checkOut:edit')")
    @Log(title = "退住办理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@ApiParam("退住办理信息") @RequestBody CheckOut checkOut) {
        return toAjax(checkOutService.updateCheckOut(checkOut));
    }

    /**
     * 删除退住办理
     */
    @ApiOperation("删除退住办理")
    @PreAuthorize("@ss.hasPermi('nursing:checkOut:remove')")
    @Log(title = "退住办理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@ApiParam("退住办理ID数组") @PathVariable Long[] ids) {
        return toAjax(checkOutService.deleteCheckOutByIds(ids));
    }

    /**
     * 申请退住
     */
    @ApiOperation("申请退住")
    @Log(title = "退住办理", businessType = BusinessType.INSERT)
    @PostMapping("/apply")
    public AjaxResult apply(@ApiParam("申请退住请求DTO") @RequestBody CheckOutApplyDto checkOutApplyDto) {
        if (checkOutApplyDto.getCheckInId() == null) {
            return AjaxResult.error("入住记录ID不能为空，请先选择老人");
        }
        checkOutService.apply(checkOutApplyDto);
        return AjaxResult.success("申请成功");
    }

    /**
     * 获取退住详细信息
     */
    @ApiOperation("获取退住详细信息")
    @GetMapping(value = "/detail/{id}")
    public R<CheckOutDetailVo> getCheckOutInfo(@ApiParam("退住记录ID") @PathVariable("id") Long id) {
        return R.ok(checkOutService.getCheckOutInfo(id));
    }

    /**
     * 审批退住申请
     */
    @ApiOperation("审批退住申请")
    @PreAuthorize("@ss.hasPermi('nursing:checkOut:approve')")
    @Log(title = "退住办理", businessType = BusinessType.UPDATE)
    @PostMapping("/approve")
    public AjaxResult approve(
            @ApiParam("退住记录ID") @RequestParam("id") Long id,
            @ApiParam("审批状态(1=已批准,3=已拒绝)") @RequestParam("status") Integer status,
            @ApiParam("审批备注") @RequestParam(value = "approvalRemark", required = false) String approvalRemark) {
        checkOutService.approve(id, status, approvalRemark);
        return AjaxResult.success("审批成功");
    }

    /**
     * 完成退住
     */
    @ApiOperation("完成退住")
    @PreAuthorize("@ss.hasPermi('nursing:checkOut:complete')")
    @Log(title = "退住办理", businessType = BusinessType.UPDATE)
    @PostMapping("/complete/{id}")
    public AjaxResult completeCheckOut(@ApiParam("退住记录ID") @PathVariable("id") Long id) {
        checkOutService.completeCheckOut(id);
        return AjaxResult.success("退住完成");
    }

    /**
     * 撤销退住申请
     */
    @ApiOperation("撤销退住申请")
    @Log(title = "退住办理", businessType = BusinessType.DELETE)
    @DeleteMapping("/revoke/{id}")
    public AjaxResult revokeCheckOut(@ApiParam("退住记录ID") @PathVariable("id") Long id) {
        checkOutService.revokeCheckOut(id);
        return AjaxResult.success("撤销成功");
    }

    /**
     * 级联删除退住和入住数据
     */
    @ApiOperation("级联删除退住和入住数据")
    @Log(title = "退住办理", businessType = BusinessType.DELETE)
    @DeleteMapping("/deleteWithCheckIn/{checkOutId}/{checkInId}")
    public AjaxResult deleteCheckOutAndCheckIn(@ApiParam("退住记录ID") @PathVariable("checkOutId") Long checkOutId,
                                              @ApiParam("入住记录ID") @PathVariable("checkInId") Long checkInId) {
        checkOutService.deleteCheckOutAndCheckIn(checkOutId, checkInId);
        return AjaxResult.success("删除成功");
    }
} 